<?php
namespace app\helpers;
use app\traits\JsonResponse;
use think\facade\Cache;

class MyCodeService{
    use JsonResponse;
    public function send($phone,$templateCode){
        $code = $this->makeCode($phone);
        if(config('app.app_env') == 'product'){
            app(AliYunService::class)->sms($phone,$templateCode,['code'=>$code]);
        }
        return $code;
    }

    protected function makeCode($phone){
        if(!empty(Cache::get($phone))) abort(403,'请稍后再试~');
        Cache::set($phone,$code = mt_rand(1000,9999),(int)config('sms.code_out_time'));
        return $code;
    }
    public function checkCode($phone,$code){
        if(!empty($cache_code = Cache::get($phone))){
            if($cache_code == $code){
                return true;
            }
        }
        return false;
    }
}